/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

package zaizai.com;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

/**
 * Toast 工具类
 */
public class Toast {
    /**
     * 显示toast
     *
     * @param context  上下文
     * @param toastStr toast 字符串
     */
    public static void showToast(Context context, String toastStr) {
        Text textComponent = new Text(context);
        textComponent.setText(toastStr);

        // 设置间距为10vp
        textComponent.setPadding(60, 40, 60, 40);
        textComponent.setTextColor(Color.BLACK);
        textComponent.setWidth(500);
        textComponent.setTextAlignment(TextAlignment.CENTER);
        textComponent.setTextSize(AttrHelper.fp2px(16, context));

        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setShape(ShapeElement.RECTANGLE);
        shapeElement.setCornerRadius(50);
        // 设置背景半透明
        shapeElement.setRgbColor(RgbColor.fromArgbInt(Color.argb(180, 255, 255, 255)));
        textComponent.setBackground(shapeElement);
        // 设置文字允许多行
        textComponent.setMultipleLine(true);

        DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig();
        layoutConfig.width = ComponentContainer.LayoutConfig.MATCH_CONTENT;
        layoutConfig.height = ComponentContainer.LayoutConfig.MATCH_CONTENT;
        layoutConfig.alignment = LayoutAlignment.CENTER;
        layoutConfig.setMarginBottom(220);
        textComponent.setLayoutConfig(layoutConfig);

        ToastDialog toastDialog = new ToastDialog(context)
                .setDuration(500)
                .setComponent(textComponent)
                .setAlignment(LayoutAlignment.BOTTOM);
        // 设置弹框背景透明
        toastDialog.setTransparent(true);
        toastDialog.show();
    }

}
